<?php
namespace app\admin\controller;
use think\Controller;
use think\Request; 
use \think\Db;
class Login extends Controller
{
      public function _initialize() {
        $request= Request::instance();
        header("Access-Control-Allow-Origin:*");
        header('Content-Type:application/json;charset=utf8');
        header("Access-Control-Allow-Headers: Origin, X-Requested- With, Content-Type, Accept");
        //获取前端传过来得参数
         $this->data = Request::instance()->param();   
      }
    

    public function index(){   
    	return $this->fetch();
    }

     /*
  *登录系统
   */


   public function login(){
    	$data =$this->data; 
    	$check = $this->validate($data,'Login.login');
        if($check !==true){
          return api_return($check);
        } 
        $result = model("Login")->checkLogin($data); 
        if($result){
          $this->success("登陆成功","Index/index");

        }else{
            $this->error("账号密码错误");
        }
   }


    /***
     * 后台退出登录
     * @param array $data
     * @return bool
     */
    public function loginOut($data=[]){
       $user = session("user_id");  
       $where = ["id"=>$user['id']];
        $result = Db::name('sys_user')->where($where)->value("id");
        if($result){
           set_cache("user_".$user['id'],null);
           session("user_id",null);
            $this->success("退出成功","Login/index");
        }else{
           $this->error("操作失败"); 
        }
    }

}